package com.forcht.client.view;


import com.forcht.client.Client;
import com.forcht.common.domain.GroupChat;
import com.forcht.common.domain.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

/**
 * Created by forcht on 2018/4/12.
 */
public class FriendsListView extends JFrame implements ActionListener,MouseListener,WindowListener {

    //处理第一张卡片，我的好友
    private JPanel fPanel,friendsPanel;
    private JButton fButton1,fButton2;
    private JScrollPane fScrollPane;
    private JLabel[] friendsLabel;
    //第二张卡片，我的群聊
    private JPanel gPanel,groupPanel;
    private JButton gButton1,gButton2;
    private JScrollPane gScrollPane;
    private JLabel[] groupsLabel;

    //把整个JFrame设置成CardLayout
  	private CardLayout cardLayout;

    private Client client;

    public FriendsListView(Client client){
        this.client=client;

        this.initFriendsCard();
        this.initGroupCard();

        //添加窗口监听
        this.addWindowListener(this);

        cardLayout=new CardLayout();
        this.setLayout(cardLayout);

        this.add(fPanel,"1");
        this.add(gPanel,"2");

        //在窗口显示自己的编号
        this.setTitle(client.getUser().getUsername()+":我的好友");
        this.setSize(300, 450);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

	public static void main(String[] args) {
		new FriendsListView(null);
	}

    /**
     * 初始化好友列表
     */
    public void initFriendsCard(){
        fPanel =new JPanel(new BorderLayout());
        friendsPanel =new JPanel(new GridLayout(30, 1, 2, 2));
        fButton1=new JButton("我的好友");
        fButton1.addActionListener(this);
        fButton2=new JButton("我的群聊");
        fButton2.addActionListener(this);
        //获取好友列表
        List<User> friends = client.getUser().getFriends();
        this.friendsLabel =new JLabel[friends.size()];
        //初始化好友列表
        for(int i = 0; i< this.friendsLabel.length; i++){
            this.friendsLabel[i]=new JLabel();
            this.friendsLabel[i].setEnabled(false);
            this.friendsLabel[i].setText(friends.get(i).getUsername());
            //判断是否在线
            if(friends.get(i).isOnline()){
                this.friendsLabel[i].setEnabled(true);
            }
            this.friendsLabel[i].addMouseListener(this);
            friendsPanel.add(this.friendsLabel[i]);
        }
        fScrollPane =new JScrollPane(friendsPanel);
        fPanel.add(fScrollPane,"Center");
        fPanel.add(fButton1,"North");
        fPanel.add(fButton2,"South");
    }

    /**
     * 初始化群聊列表
     */
    public void initGroupCard(){
        gPanel =new JPanel(new BorderLayout());
        groupPanel =new JPanel(new GridLayout(30, 1, 2, 2));
        gButton1=new JButton("我的群聊");
        gButton2=new JButton("我的好友");
        gButton1.addActionListener(this);
        gButton2.addActionListener(this);
        //获取群聊列表
        List<GroupChat> groups=client.getUser().getGroupChats();
        this.groupsLabel =new JLabel[groups.size()];
        //初始化群聊列表
        for(int i = 0; i< this.groupsLabel.length; i++){
            this.groupsLabel[i]=new JLabel();
            this.groupsLabel[i].setText(groups.get(i).getName());

            this.groupsLabel[i].addMouseListener(this);
            groupPanel.add(this.groupsLabel[i]);
        }
        gScrollPane =new JScrollPane(groupPanel);
        gPanel.add(gScrollPane,"Center");
        gPanel.add(gButton1,"North");
        gPanel.add(gButton2,"South");
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getButton()==1&&e.getClickCount()==2){
            JLabel source = (JLabel) e.getSource();
            String chatUsername=source.getText();
            ChatView chatView = new ChatView(client,chatUsername);
            client.addChatView(chatUsername,chatView);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel jl=(JLabel)e.getSource();
        jl.setForeground(Color.red);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        JLabel jl=(JLabel)e.getSource();
        jl.setForeground(Color.black);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //如果是点击了我的好友的群聊按钮，则显示群聊列表
        if(e.getSource()==fButton2){
            cardLayout.show(this.getContentPane(),"2");
        }
        //如果是点击了我的群聊的好友列表，则显示我的好友列表
        else if(e.getSource()==gButton2){
            cardLayout.show(this.getContentPane(),"1");
        }
    }

    /**
     * 更新好友的状态
     * @param username
     * @param isOnline
     */
    public void updateFriendStatus(String username,boolean isOnline){
        for(int i=0;i<friendsLabel.length;i++){
            JLabel jLabel = friendsLabel[i];
            String text = jLabel.getText();
            if(text.equals(username)){
                jLabel.setEnabled(isOnline);
                break;
            }
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        //发送下线的信息
        client.logout();
        //等消息发送出去
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        //关闭客户端
        client.close();
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
